package com.memory.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.memory.api.mapper.InterfaceInfoMapper;
import com.memory.api.service.UserInterfaceInfoService;
import com.memory.common.common.IdRequest;
import com.memory.common.model.entity.InterfaceInfo;
import com.memory.common.model.entity.UserInterfaceInfo;
import com.memory.api.annotation.AuthCheck;
import com.memory.common.common.BaseResponse;
import com.memory.common.common.ErrorCode;
import com.memory.common.common.ResultUtils;
import com.memory.common.exception.BusinessException;
import com.memory.api.mapper.UserInterfaceInfoMapper;
import com.memory.common.model.vo.InterfaceInfoVO;
import com.memory.api.service.InterfaceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分析控制器
 */
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;
    @Resource
    private InterfaceInfoService interfaceInfoService;
    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    /**
     * 统计分析接口调用排行榜(热门接口)
     *
     * @return 热门接口
     */
    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfo(int limit) {
        // 获取被调用次数最多的接口
        List<InterfaceInfo> interfaceInfoList = interfaceInfoMapper.listTopInvokeInterfaceInfo(limit);

        List<InterfaceInfoVO> interfaceInfoVOList = interfaceInfoList.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            return interfaceInfoVO;
        }).collect(Collectors.toList());

        return ResultUtils.success(interfaceInfoVOList);
    }

    /**
     * 获取过去七天的调用记录
     *
     * @param userId 用户 id
     * @return 接口列表
     */
    @GetMapping("/latest/interface/invoke")
    public BaseResponse<List<InterfaceInfoVO>> listLatestInvokeInterfaceInfo(long userId) {
        List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoService.listByUserId(userId);

        Map<Long, List<UserInterfaceInfo>> interfaceInfoIdObjMap = userInterfaceInfoList.stream()
                .collect(Collectors.groupingBy(UserInterfaceInfo::getInterfaceInfoId));

        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", interfaceInfoIdObjMap.keySet());

        List<InterfaceInfo> interfaceInfoList = interfaceInfoService.list(queryWrapper);
        List<InterfaceInfoVO> interfaceInfoVOList = interfaceInfoList.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);

            return interfaceInfoVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(new ArrayList<>());
    }
}
